#include "aerial\BattleScene.h"

// fly object
#include "aerial\Probe.h"
// weapon
#include "aerial\Beam.h"
#include "aerial\Vulcan.h"

#include "aerial\Core\PlayerDescriptor.h"

#include "aerial\Scene\SceneManager.h"

#include <iostream>

using namespace std;

BattleScene::BattleScene()
{
	memset(fObjs, 0, sizeof(fObjs));
	playerCount = 0;
	lastMouseStatus = false;

	hiEvent = new hgeInputEvent;
}

BattleScene::~BattleScene()
{
	delete Beam::spr;

	delete hiEvent;
}

bool BattleScene::initialize()
{
	//TODO initialize players array
	//player = new Probe(hgeResMan, hge, netIO, 300, 400);

	player = new Scout(hgeResMan, hge, 300, 400);

	sceneManager->addObject(player);
	Beam::spr = new hgeSprite(hgeResMan->GetTexture("tex.beam1"), 41.f, 64.f, 41.f, 64.f);
	return true;
}

bool BattleScene::update(float dt)
{
	//Packet* p;
	//netIO->recv(p);
	// p->data[1] specified player id
	//if (p && p->data[1] < playerCount) fObjs[p->data[1]]->processPacket(p);

	hge->Input_GetMousePos(&mouseX, &mouseY);
	mouseX += player->x - 400;
	mouseY += player->y - 300;

	hgeVector d;
	float ctime = hge->Timer_GetTime();

	// >>>> fwh
	while (hge->Input_GetEvent(hiEvent))
	{
		if (hiEvent->key == HGEK_SHIFT)
			if (hiEvent->type == INPUT_KEYDOWN)
				player->mirageOn();
			else
				player->mirageOff();
		if (hiEvent->key == HGEK_Z)
			if (hiEvent->type == INPUT_KEYDOWN)
				player->boostOn();
			else
				player->boostOff();
		else if (hiEvent->type == INPUT_MOUSEMOVE &&
			hge->Input_GetKeyState(HGEK_LBUTTON))
		{
			player->fly(mouseX, mouseY);
		}
		else if (hiEvent->type == INPUT_MBUTTONDOWN &&
			hiEvent->key == HGEK_LBUTTON)
		{
			player->fly(mouseX, mouseY);
		}
		else if (hiEvent->type == INPUT_MBUTTONUP &&
			hiEvent->key == HGEK_LBUTTON)
			player->flyOff();
	}
	//if ()
	// <<<<

	//if (hge->Input_GetKeyState(HGEK_RBUTTON))
	//{
	//	player->setShieldStatus(true);
	//} else {
	//	player->setShieldStatus(false);
	//}
	//if (hge->Input_GetKeyState(HGEK_W)) {
	//	player->fly(dt);
	//}
	//if (hge->Input_GetKeyState(HGEK_A)) {
	//	player->cclockwiseRotate(dt);
	//}
	//if (hge->Input_GetKeyState(HGEK_D)) {
	//	player->clockwiseRotate(dt);
	//}

	//if (hge->Input_GetKeyState(HGEK_LBUTTON) ^ lstatus) {
	//	if (Beam::cd(ctime, 0))
	//	{
	//		d.x = mouseX - player->x;
	//		d.y = mouseY - player->y;
	//		d.Normalize();
	//		ifcs.addWeapon(new Beam(hgeResMan, player->x, player->y, player->rot, ctime, 0)); 
	//	} 
	//}
	ifcs.Update(dt);
	lastMouseStatus = hge->Input_GetKeyState(HGEK_LBUTTON);
	return false;
}

bool BattleScene::render(float dt)
{
	hge->Gfx_Clear(0);
	hge->Gfx_SetTransform(0.0f, 0.0f, 400 - player->x, 300 - player->y, 0.0f, 1.0f, 1.0f);
	hgeResMan->GetSprite("spr.bg")->Render(0, 0);
	ifcs.Render();
	return false;
}
